package org.soenea.doitall.domain.model.group.data;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.dsrg.soenea.service.threadLocal.DbRegistry;

public abstract class GroupTDG {
	public static final String BASE_NAME = "group";
	public static final String TABLE = DbRegistry.getTablePrefix()+BASE_NAME;
	public static final String INSERT = "INSERT INTO " + TABLE + " (groupId,version,groupName,description) values(?,?,?,?);";
	// FIXME: UPDATE query is not complete
	public static final String UPDATE = "UPDATE " + TABLE + " SET version=version+1,groupName=?, description=? WHERE groupId=? and version=?;";
	public static final String DELETE = "DELETE FROM " + TABLE + " WHERE groupId=? AND version=?;";
	public static final String GROUP_ID = "groupId";
	
	public static void insert(long id, long version, String name, String description) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(INSERT);
		ps.setLong(1, id);
		ps.setLong(2, version);
		ps.setString(3, name);
		ps.setString(4,description);
		ps.executeUpdate();
		ps.close();
	}

	public static int update(long id, long version, String name, String description) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(UPDATE);
		ps.setString(1, name);
		ps.setString(2, description);
		ps.setLong(3, id);
		ps.setLong(4, version);		
		int count = ps.executeUpdate();
		ps.close();
		return count;
	}

	public static int delete(long id, long version) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(DELETE);
		ps.setLong(1, id);
		ps.setLong(2, version);
		int count = ps.executeUpdate();
		ps.close();
		return count;
	}
}
